package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderEvaluateDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderEvaluateSaveDTO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderEvaluateSaveVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderEvaluateVO;
import com.xbongbong.saas.service.WorkOrderEvaluationService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * @author 刘阳
 * @date 2019/4/15 3:19 PM
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/workOrder/center")
public class MobileWorkOrderEvaluationController {

    @Resource
    private WorkOrderEvaluationService workOrderEvaluationService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    /**
     * Description: 查看评价
     * @param workOrderEvaluateDTO
     * @return java.lang.String
     * @author 刘阳
     * @date 2019/4/15 9:46 AM
     * @since v1.0
     */
    @RequestMapping(value ="/getEvaluation",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEvaluation(@RequestBody @Valid WorkOrderEvaluateDTO workOrderEvaluateDTO, BindingResult br) {
        XbbResponse<WorkOrderEvaluateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderEvaluateVO workOrderEvaluateVO = workOrderEvaluationService.getEvaluation(workOrderEvaluateDTO);
                response = new XbbResponse<>(workOrderEvaluateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 评价工单
     * @param workOrderEvaluateSaveDTO
     * @return java.lang.String
     * @author 刘阳
     * @date 2019/4/15 3:09 PM
     * @since v1.0
     */
    @RequestMapping(value = "/evaluate/save",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String evaluateSave(@RequestBody @Valid WorkOrderEvaluateSaveDTO workOrderEvaluateSaveDTO, BindingResult br) {
        XbbResponse<WorkOrderEvaluateSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(workOrderEvaluateSaveDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }

                WorkOrderEvaluateSaveVO workOrderEvaluateSaveVO = workOrderEvaluationService.evaluateSave(workOrderEvaluateSaveDTO);
                response = new XbbResponse<>(workOrderEvaluateSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
